package com.group.three.sdk;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class WxPayService {
    /**
     * * 微信支付native支付模式
     *
     * @param orderId 订单id
     * @param price   订单价格，单位分
     */
    public Map<String, Object> nativePay(String orderId, int price) throws Exception {
        //1.准备微信支付环境
        WXPayConfig wxPayConfig = new WXPayConfigImpl();
        WXPayRequest wxPayRequest = new WXPayRequest(wxPayConfig);
        //2.准备微信支付参数
        Map<String, String> metadata = new HashMap<>();
        //公众号id
        metadata.put("appid", wxPayConfig.getAppID());
        //商户id
        metadata.put("mch_id", wxPayConfig.getMchID());
        //随机字符串，主要用于表示当前唯一的交易码
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        metadata.put("nonce_str", uuid);
        //商品的描述，一般是一个json字符串{名称: java课程,类别:java}
        metadata.put("body", "课工场java项目");
        //商品订单唯一识别码，注意一般是一个有一定规律的字符串
        metadata.put("out_trade_no", orderId);
        //交易金额，单位分
        metadata.put("total_fee", "1");
        //设备ip
        metadata.put("spbill_create_ip", "112.23.168.130");
        //微信支付结果回调地址
        metadata.put("notify_url", "http://xqqzg5.natappfree.cc/wx/receiveWxPay");
        //支付模式，本样例固定是NATIVE
        metadata.put("trade_type", "NATIVE");
        //3.生成签名
        String sign = WXPayUtil.generateSignature(
                metadata,
                wxPayConfig.getKey(),
                WXPayConstants.SignType.MD5);
        metadata.put("sign", sign);
        //将信息转换为xml格式，用于向微信支付系统发送标准化数据
        String mapToXml = WXPayUtil.mapToXml(metadata);
//        System.out.println("----------------------");
//        System.out.println(mapToXml);
        //4.发送微信支付数据
        String reportRes = wxPayRequest.requestWithoutCert(
                "/pay/unifiedorder",
                uuid,
                mapToXml,
                true

        );
        System.out.println("----------------------");
        System.out.println(reportRes);
        //5.解析微信支付系统返回结果，获取code_url用于生成二维码
        Map<String, String> res = WXPayUtil.xmlToMap(reportRes);
//        System.out.println("----------------------");
//        System.out.println(stringStringMap);
        //6.如果微信支付的信息正确，将code_url生成二维码返回给页面
        String return_code = res.get("return_code");
        Map<String, Object> returnRes = new HashMap<>();
        if ("SUCCESS".equalsIgnoreCase(return_code)) {
            String code_url = res.get("code_url");
            returnRes.put("codeUrl", code_url);
            returnRes.put("code", "200");
        } else {
            returnRes.put("code", "400");

        }
        return returnRes;
    }

    /**
     * * 根据微信商户订单号来主动查询该订单的支付状态
     *
     * @param out_trade_no 商户订单号
     */
    public void selectWxOrder(String out_trade_no) throws Exception {
        //1.构建查询环境
        WXPayConfig wxPayConfig = new WXPayConfigImpl();
        WXPayRequest wxPayRequest = new WXPayRequest(wxPayConfig);
        //2.构建查询参数
        Map<String, String> metadata = new HashMap<>();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        metadata.put("appid", wxPayConfig.getAppID());
        metadata.put("mch_id", wxPayConfig.getMchID());
        metadata.put("out_trade_no","kgc20230103100710M1001");
        metadata.put("nonce_str", uuid);
        //3.格式化签名和参数
        String sign = WXPayUtil.generateSignature(
                metadata,
                wxPayConfig.getKey(),
                WXPayConstants.SignType.MD5);
        metadata.put("sign", sign);
        String mapToXml = WXPayUtil.mapToXml(metadata);
        //4.发送查询信息，获取查询结果
        String reportRes = wxPayRequest.requestWithoutCert(
                "/pay/orderquery",
                uuid,
                mapToXml,
                true
        );
        System.out.println("---------------------");
        System.out.println(reportRes);
        Map<String, String> res = WXPayUtil.xmlToMap(reportRes);
        System.out.println("---------------------");
        System.out.println(res);
    }

    public static void main(String[] args) throws Exception {
        WxPayService wxPayService = new WxPayService();
//        wxPayService.nativePay("1", 111);
        wxPayService.selectWxOrder("kgc20230103100710M1001");
    }
}
